/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;

public class HardDiskObserver extends Observer implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private Date date;

	 private String path;

	 private long totalBytes;

	 private long usableBytes;

	 private long freeBytes;

	 private double usedBytes;

	 HardDiskObserver() {
			super();
	 }

	 //Test
	 HardDiskObserver(Date date, String context, String path, long totalBytes, long usableBytes, long freeBytes, double usedBytes) {
			super(context);
			this.date = date;
			this.path = path;
			this.totalBytes = totalBytes;
			this.usableBytes = usableBytes;
			this.freeBytes = freeBytes;
			this.usedBytes = usedBytes;
	 }

	 synchronized void invoke() {

			if (JeeObserverContext.getInstance() != null) {

				 this.date = new Date(System.currentTimeMillis());

				 // Iterate all roots
				 final File roots[] = File.listRoots();

				 for (final File file : roots) {
						this.path = file.getPath();

						this.totalBytes = file.getTotalSpace();
						this.usableBytes = file.getUsableSpace();
						this.freeBytes = file.getFreeSpace();

						this.usedBytes = this.totalBytes - this.usableBytes;

						JeeObserverContext.getInstance().addObserver(this);
				 }

			} else {
				 JeeObserverContext.logger.log(Level.FINE, "JeeObserver context not exists.");
			}
	 }

	 public Date getDate() {
			return date;
	 }

	 public long getTotalBytes() {
			return totalBytes;
	 }

	 public void setTotalBytes(long totalBytes) {
			this.totalBytes = totalBytes;
	 }

	 public long getUsableBytes() {
			return usableBytes;
	 }

	 public void setUsableBytes(long usableBytes) {
			this.usableBytes = usableBytes;
	 }

	 public long getFreeBytes() {
			return freeBytes;
	 }

	 public void setFreeBytes(long freeBytes) {
			this.freeBytes = freeBytes;
	 }

	 public double getUsedBytes() {
			return usedBytes;
	 }

	 public void setUsedBytes(double usedBytes) {
			this.usedBytes = usedBytes;
	 }

	 public String getPath() {
			return this.path;
	 }

	 public void setPath(String path) {
			this.path = path;
	 }
}
